<?php
/**
 * 配货单	单号$number_code 可以是订单号，也可以是下架单号
 * 下架单	单号$number_code 为下架单号
 * 入库单	单号$number_code 为 ASN单号
 * 上架单	单号$number_code 为上架清单号
 * 物流单号	单号$number_code可以是订单号也可以是物流的追踪号
 * 热敏打印	单号$number_code 为订单号，也可以是下架单号
 */


class Warehouse_Service_ReprintProccess
{

	public static function getDocument($numberCode,$type)
	{ 
		$number_code = $numberCode;
		$data = array('ask'=>0,'msg'=>'There are no data.');
    	$row = $data['result'] = array();
    	if($type == 'orders'){	// 重新打印配货单
    		$processStatus = Order_Service_Order::getAllProcessStatus();	//订单后台状态   
    		//$number_code 可以是订单号，也可以是下架单号
    		if($number_code{0} == 'W'){
    			$row = Order_Service_Order::getByOrdersCode($number_code);
    			if(isset($row['process_status']) && $row['process_status'] < 3){	 //3:已下架 。已下架状态之后才可打印
    				$data['msg'] = '产品已下架才可以打印';
    				die(Zend_Json::encode($data));
    			}
    			if (count($row)){
    				$row['number_code'] = $row['orders_code'];
    				$row['shipping_method'] = $row['shipping_method'];
    				$row['status'] = $processStatus[$row['process_status']];    				
    				$row['ref_id'] = '';
    				$row['type'] = '订单号';
    				$row['create_time'] = $row['add_time'];
    				$data['result'][] = $row;
    				$data['ask'] = 1;
    				$data['msg'] = 'Success';
    			}
    			    			
    		}elseif ($number_code{0} == 'U'){
    			//如果$number_code是下架单号，从order_pickup_map表中查orders_code
    			$cond = array('op_code'=>$number_code);    			
    			$rows = Product_Service_Pickup::getPickupMap($cond);
    			$rowList = array();
    			$orderCodeArr = array()	;	
    			foreach($rows as $key => $val){
    				if(in_array($val['orders_code'],$orderCodeArr)){
    					continue;
    				}
    				$orderCodeArr[] = $val['orders_code'];
					$row = Order_Service_Order::getByOrdersCode($val['orders_code']);
					if (count($row)){
    					$row['number_code'] = $row['orders_code'];
    					$row['shipping_method'] = $row['shipping_method'];
    					$row['status'] = $processStatus[$row['process_status']];
    					$row['ref_id'] = '';
    					$row['type'] = '订单号';
    					$row['create_time'] = $row['add_time'];
    					$rowList[$row['shipping_method']][] = $row;
    					$data['ask'] = 1;
    					$data['msg'] = 'Success';
    				}
				}
				ksort($rowList);
				foreach ($rowList as $key => $val){
					foreach ($val as $key2 => $val2){
						$data['result'][] = $val2;
					}					
				}
    			
    		}elseif (strtoupper($number_code{0}) == 'L'){
    			$rows = Order_Service_OrderShip::getByfield('ref_tracking_number',$number_code);
    			if (count($rows[0])){
    				$row = Order_Service_Order::getByOrdersCode($rows[0]['orders_code']);
					
    				if(isset($row['process_status']) && $row['process_status'] < 3){	 //3:已下架 。已下架状态之后才可打印
    					$data['msg'] = '产品已下架才可以打印';
    					die(Zend_Json::encode($data));
    				}
    				if (count($row)){
    					$row['number_code'] = $row['orders_code'];
    					$row['shipping_method'] = $row['shipping_method'];
    					$row['status'] = $processStatus[$row['process_status']];    				
    					$row['ref_id'] = '';
    					$row['type'] = '订单号';
    					$row['create_time'] = $row['add_time'];
    					$data['result'][] = $row;
    					$data['ask'] = 1;
    					$data['msg'] = 'Success';
    				}
    			}
    			
    		}
    		
    	}
    	
    	if($type == 'order_pickup'){		// 重新打印下架单
    		$pickupStatus = Product_Service_Pickup::$pickupStatus;
    		$cond = array('op_code'=>$number_code);		//	$number_code 下架单号
    		$rows = Product_Service_Pickup::getByCondition($cond);
    		if(count($rows)>0){
    			$row = $rows[0];    			
    			if(count($row)){
    				$row['number_code'] = $row['op_code'];
    				$row['status'] = $pickupStatus[$row['op_status']];
    				$row['ref_id'] = '';
    				$row['type'] = '下架单';
    				$row['create_time'] = $row['op_create_date'];
					$data['result'][] = $row;
					$data['ask'] = 1;
					$data['msg'] = 'Success';
				}
    		}
    		
    	}
    	
    	if($type == 'receiving'){	// 重新打印入库单
    		$statusText = Product_Service_Receiving::$statusText;
    		$receivingCode = $number_code;	// $number_code 为 ASN单号
    		$condition = array("receiving_code" => $receivingCode);

			$rows = Product_Service_Receiving::getByCondition($condition);
			if(count($rows)>0){
    			$row = $rows[0];    			
    			if(count($row)){
    				$row['number_code'] = $row['receiving_code'];
    				$row['status'] = $statusText[$row['status']];
    				$row['ref_id'] = $row['ref_id'];
    				$row['type'] = '入库单';
    				$row['create_time'] = $row['created'];
					$data['result'][] = $row;
					$data['ask'] = 1;
					$data['msg'] = 'Success';
				}
    		}
    		
    	}
    	
    	if($type == 'putaway'){		// 重新打印上架单
    		$putaway_status = Product_Service_Putaway::$putaway_status;
    		$contion=array('putaway_code' => $number_code,);	//$number_code 上架清单号
    		$rows = Product_Service_Putaway::getByCondition($contion,"*");
    		if(count($rows)>0){
    			$row = $rows[0];    			
    			if(count($row)){
    				$row['number_code'] = $row['putaway_code'];
    				$row['status'] = $putaway_status[$row['putaway_status']];
    				$row['ref_id'] = '';
    				$row['type'] = '上架单';
    				$row['create_time'] = $row['putaway_create_date'];
					$data['result'][] = $row;
					$data['ask'] = 1;
					$data['msg'] = 'Success';
				}
    		}
    		
    	}
    	
    	if($type == 'ship_number'){	// 重新打印 物流单号；
    		//$number_code可以是订单号也可以是物流的追踪号
    		if($number_code{0} == 'W'){
    			$tmp = Order_Service_Order::getByOrdersCode($number_code);
    			if($tmp['process_status'] < 4){	 //4:待发货 。待发货状态之后才可打印
    				$data['msg'] = '订单待发货才可以打印物流单';
    				die(Zend_Json::encode($data));
    			}
    			$rows = Order_Service_OrderShip::getLastRowByOrdersCode($number_code);     			    			
    		}elseif ($number_code{0} == 'Q'){
    			$rows = Order_Service_OrderShip::getByfield('tracking_number',$number_code);
//     			print_r($rows);exit;
    			if($rows){
    				$rows = $rows[0];    				
    			}
    		}
    		if ($rows){
    			$row = $rows;
    			$row['number_code'] = $row['tracking_number'];
    			$tmpStatus = Order_Service_OrderShip::getOrderStatus($row['os_status']);
    			$row['status'] = $tmpStatus;
    			$row['ref_id'] = $row['orders_code'];
    			$row['type'] = '物流单号';
    			$row['create_time'] = $row['os_create_date'];
    			$data['result'][] = $row;
    			$data['ask'] = 1;
    			$data['msg'] = 'Success';
    		}
    	}
    	
    	if($type == 'hot_print'){	// 重新 热敏打印
    		$processStatus = Order_Service_Order::$processStatus;	//订单后台状态   
    		//$number_code 为订单号
    		if($number_code{0} == 'W'){
    			$row = Order_Service_Order::getByOrdersCode($number_code);
    			if(isset($row['process_status']) && $row['process_status'] < 4){	 //4:待发货 。待发货状态之后才可打印
    				$data['msg'] = '订单待发货才可以打印';
    				die(Zend_Json::encode($data));
    			}
    			if (count($row)){
    				$row['number_code'] = $row['orders_code'];
    				$row['shipping_method'] = $row['shipping_method'];
    				$row['status'] = $processStatus[$row['process_status']];
    				$row['ref_id'] = '';
    				$row['type'] = '订单号';
    				$row['create_time'] = $row['add_time'];
    				$data['result'][] = $row;
    				$data['ask'] = 1;
    				$data['msg'] = 'Success';
    			}
    			    			
    		}elseif ($number_code{0} == 'U'){
    			//如果$number_code是下架单号，从order_pickup_map表中查orders_code
    			$cond = array('op_code'=>$number_code);    			
    			$rows = Product_Service_Pickup::getPickupMap($cond);
    			$orderCodeArr = array()	;
    			$rowList = array();
    			foreach($rows as $key => $val){    				
    				if(in_array($val['orders_code'],$orderCodeArr)){
    					continue;
    				}
    				$orderCodeArr[] = $val['orders_code'];
					$row = Order_Service_Order::getByOrdersCode($val['orders_code']);
					if(isset($row['process_status']) && $row['process_status'] < 4){	 //4:待发货 。待发货状态之后才可打印
    					$data['msg'] = '订单待发货才可以打印';
    					die(Zend_Json::encode($data));
    				}
					if (count($row)){
    					$row['number_code'] = $row['orders_code'];
    					$row['shipping_method'] = $row['shipping_method'];
    					$row['status'] = $processStatus[$row['process_status']];
    					$row['ref_id'] = '';
    					$row['type'] = '订单号';
    					$row['create_time'] = $row['add_time'];
    					$rowList[$row['shipping_method']][] = $row;
    					$data['ask'] = 1;
    					$data['msg'] = 'Success';
    				}
				}
				ksort($rowList);
				foreach ($rowList as $key => $val){
					foreach ($val as $key2 => $val2){
						$data['result'][] = $val2;
					}
				}
    			
    		}elseif (strtoupper($number_code{0}) == 'L'){
    			$rows = Order_Service_OrderShip::getByfield('ref_tracking_number',$number_code);
    			if (count($rows[0])){
    				$row = Order_Service_Order::getByOrdersCode($rows[0]['orders_code']);
					
    				if(isset($row['process_status']) && $row['process_status'] < 4){	//4:待发货 。待发货状态之后才可打印
    					$data['msg'] = '订单待发货才可以打印';
    					die(Zend_Json::encode($data));
    				}
    				if (count($row)){
    					$row['number_code'] = $row['orders_code'];
    					$row['shipping_method'] = $row['shipping_method'];
    					$row['status'] = $processStatus[$row['process_status']];    				
    					$row['ref_id'] = '';
    					$row['type'] = '订单号';
    					$row['create_time'] = $row['add_time'];
    					$data['result'][] = $row;
    					$data['ask'] = 1;
    					$data['msg'] = 'Success';
    				}
    			}
    			
    		}
    		
    	}
    	
    	if($type == 'qc_print'){		//	QC打印
    		$cond = array();
    		if(strtoupper($number_code{0}) == 'Q'){//2012年7月16日 欧弘涛修正 
    			$cond['qco_code'] = $number_code;			
    		}else{    			
    			$cond['qco_ref_code'] = $number_code;
    		}
    		$rows = Product_Service_QualityControlOrder::getByCondition($cond,'*',array('qco_id'),0,0);
    		if (count($rows)){
    			foreach ($rows as $key => $row){
    				$row['number_code'] = $row['qco_code'];
    				$row['shipping_method'] = '';
    				$row['status'] = $row['statusText'];    				
    				$row['ref_id'] = $row['qco_ref_code']; 
    				$row['type'] = 'QC单号';
    				$row['create_time'] = $row['qco_create_time'];
    				$data['result'][] = $row;
    			}
    			$data['ask'] = 1;
    			$data['msg'] = 'Success';
    			
    		}
    		
    	}
    	
    	return $data;
		
	}
	
	/**
	 * 热敏打印的 
	 */

	
	public  static function createLog($orderCode,$remark='',$status=3){
		$session = new Zend_Session_Namespace('userAuth');
		$userid=isset($session->userID)?$session->userID:0;
		$logRow = array (
				"orders_code" => $orderCode,
				"osh_type" => "Process",
				"osh_statu" => $status,
				"osh_create_date" => date("Y-m-d H:i:s"),
				"osh_comments" => "order re-print",
				"osh_remark" => $remark,
				"user_id" => $userid,
				"osh_ip" => $_SERVER['REMOTE_ADDR'],
		);
		$model = new Order_Model_History();
		$model->create($logRow);
			
	}
	
	/**
	 * 创建QC单重新打印的日志
	 *
	 */
	public  static function createQclog($numberCode){
		$number_code = $numberCode;
		$session = new Zend_Session_Namespace('userAuth');
		$userId = isset($session->userID)?$session->userID:0;
		
		$productService = new Product_Service_Product();
		$adapter = $productService->getAdapter();
		$adapter->beginTransaction();
		$data = array('ask'=>0,'msg'=>array(' 打印失败 '));
		
		try{
			$cond = array();
    		if(strtoupper($number_code{0}) == 'Q' ){    			
    			$cond['qco_code'] = $number_code; 			
    		}else {
    			$cond['qco_ref_code'] = $number_code;     			    			    	
    		}
    		$rows = Product_Service_QualityControlOrder::getByCondition($cond,'*',array('qco_id'),0,0);
    		if (count($rows)){
    			$logObj = new Product_Model_ReceivingLog();
    			foreach ($rows as $key => $row){
    				$asnInfo = Product_Service_Receiving::getByReceivingCode($row['qco_ref_code']);
    				$logRow = array();
    				$logRow['receiving_code'] = $row['qco_ref_code']; 
    				$logRow['user_id'] = $userId;
    				$logRow['rl_statu_now'] = $asnInfo['status'];
    				
    				$logRow['rl_note'] = 'QC单重新打印';
    				$logRow['rl_add_time'] = date('Y-m-d H:i:s');
    				$logRow['rl_ip'] = $_SERVER['REMOTE_ADDR'];
    				
					$res = $logObj->add($logRow);	
    			}
    		}
    		$data['ask'] = 1;
    		$data['msg'] = array('Success');
    		
			$adapter->commit();			
		}catch(Exception $e){
			$adapter->rollback();			
			return array('ask'=>0,'msg'=>array($e->getMessage()));		
		}
		
    	return $data;
	}
	
	
}